#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "qframe.h"
#include <qbuttongroup.h>
#include "qpushbutton.h"
#include "qprocess.h"
#include <QTime>
#include<QTimer>
#include "qlabel.h"
#include "qthread.h"
#include <QFile>
#include <qfile.h>
#include <qtextstream.h>
#include <qtextcodec.h>
#include <QDate>
#include <QFont>
#include "qextserialport.h"
#include <QFontDatabase>
#include <QRect>
#include <QFileInfo>
#include <QDir>
#include "micro_define.h"
#include "thread_class.h"

#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <unistd.h>


#define line_max_lenth 256

//#define qt_mplayer_test

namespace Ui {
    class MainWindow;
}


#include "thread_class.h"

//class ElevatorUart1Thread;
//class ElevatorUart2Thread;
class TextUartThread;
class ElevatorUart1ReadThread;
class ElevatorUart2ReadThread;



#if 0
class Update_files : public QThread {
    Q_OBJECT
    public:
    char *p0;
    char buff_in[1024];
    unsigned char buffer0[1024];
    char buff_out[16];
    QextSerialPort *port;

    Update_files();

    virtual void run();

signals:
    void emit_update(void);
    };
#endif

int Init_serial(QString u_serial);
void update_copy_info(int fd_pipe);
void start_copy_file(void);
bool removeDirectory(QString dirName);
bool AES_check(unsigned char *in_data,unsigned char *out_data);



class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    QTextCodec *gbCode;

    //QFrame *down_up_frame;
    //QFrame *down_up_frame1;

    QFrame *down_frame1;
    QFrame *down_frame2;
    QFrame *up_frame1;
    QFrame *up_frame2;


    QProcess *process0 ; // video
    QProcess *process1; //floor infor
    QProcess *cp_process;
    QLabel *label_time;
    QLabel *label_spec_info;
    QLabel *label_floor;
    QLabel *label_week;
    QLabel *label_date;
    QLabel *label_copy_schedule;


    QLabel *label_spec_info1;
    QLabel *label_floor1;

    QFrame *logo_frame;
    unsigned char mplayer_run_flag;
    qint32 q_flag0;
    qint32 move_pos;
    qint32 bottom_move_pos;
    qint32 time_flag0;
    qint32 uart_context_flag;
    qint32 flush_flag;
    qint32 video_counter;
    qint32 files_counter;
    qint32 debug_counter;
    int fd_pipe;
    int encrypt_uart_fd_1;
    int encrypt_uart_fd_2;

    char week_dis_flag;
    unsigned int  all_bytes;
    unsigned int current_read_bytes;
    unsigned char data_src_encrypt[64];
    QTimer timer0;
    QTimer *uart0_timer;
    QTimer *uart1_timer;
    QTimer *copy_file_timer;
    QPalette palette_red;
    QPalette palette_yellow;
    QPalette palette_white;



    QDate current_date;
    QDate date0;
    //qint32 ratio;
    qint32 floor_num;
    int time_out_flag1;
    int time_out_flag2;
    char direction_flag;
    char spec_info;
    MainWindow *my_window;

    unsigned char audio_info;
    QRect screenrect;
   // QPushButton *button1;
    QTime current_time;
    QStringList video_lines;
    QStringList video_times;
    QStringList update_files;
    QStringList floor_real_name;

    QString video_files;
    QString get_the_video_pos_0 = "test";
    QString get_the_video_pos_1;
    QString current_dir;
    QString audio_floor_flag;
    QString elevator_info_uart_1;
    QString elevator_info_uart_2;
    QString elevator_num;
    QString serial_num;
    QString text_uart;
    QString uart_context;//串口输入的特殊信息
    QString display_pos;
    QString system_verify_flag_1;
    QString system_verify_flag_2;

    QString source_dir;//u disk dir
    QStringList bottom_lines;
    QStringList top_lines;
    QStringList line_times;


    QextSerialPort *port;
    qint32 ratio_x,ratio_y;
    struct roll_para read_roll_para;
    struct roll_para read_roll_bottom;
    struct ElevatorInfoState elevator_info_state1;
    struct ElevatorInfoState elevator_info_state2;
    //char temp[1024];
    unsigned char data0[16];
    char data1[16];
    void new_wiget(void);
    void start_default(MainWindow *mw);
    void read_caption_file(void);
    void set_fonts(void);
    void set_widget_size(void);
    void set_widget_bg(void);
    void set_dis_config(void);
    void parse_config(void);
    unsigned char system_verify(void);
    void video_codec_check(void);
    void check_u_disk(int);
    void picture_load(void);
    void debug_emit(void);
    void new_start(void);

    //ElevatorUart1Thread    *elevator_uart1_thread;
    //ElevatorUart2Thread *elevator_uart2_thread;
    ElevatorUart1ReadThread *elevator_uart1_read_thread;
    ElevatorUart2ReadThread *elevator_uart2_read_thread;

    TextUartThread *text_uart_thread;

    explicit MainWindow(QWidget *parent = 0);
    void open_init_uart(void);
    ~MainWindow();
signals:
    void emit_move(void);
    void emit_read_fifo(void);
    void emit_updatetime(void);
    void emit_updatefloor(void);
    void emit_change_direction(void);
    void emit_update_spec_info(void);
    void emit_updatefloor2(void);
    void emit_change_direction2(void);
    void emit_update_spec_info2(void);

    void emit_roll_one_time(void);
    void emit_roll_one_time_bottom(void);
    void emit_read_uart_buf(void);
    //void emit_update_info(void);
    void emit_update_uart_1();
    void emit_update_uart_2();

    void emit_play_floor_info(void);
    void emit_system_verify(void);

private slots:
    //void on_button1_clicked();
    //void on_pushButton_clicked();
    void paintEvent(QPaintEvent *);
    void timerEvent(QTimerEvent *);
    void move_label(void);    
    void update_time(void);    

    void update_floor(void);
    void change_direction(void);
    void update_spec_info(void);
    void update_uart_context(void);
    void update_floor2(void);
    void change_direction2(void);
    void update_spec_info2(void);

    void read_fifo(void);
    void roll_disposal(void);
    void roll_disposal2(void);
    void read_uart_buf(void);
    void update_info2(void);
    void update_info1(void);

    void copy_files(void); // update files
    void copy_label_flush(void); // flush

    void play_floor_info(void);
    void video_adjust(void);
    void update_video_player(void);
    void restart_video_player(void);
    char do_system_verify(void);

    char system_timer_0(void);

    void update_copy_info(void);
    void seek_video(void);
    void get_encrypt_data(void);
    void ui_on_top(void);
    void get_mplayer_pos(void);
    void check_mplayer_stat(void);



private:
    Ui::MainWindow *ui;
    void screen_change_to_update_state(void);
    void get_floor_real_name(void);
    QString get_next_bottom_line(void);
};

#endif // MAINWINDOW_H
